
package siga.visao_busca;

import java.sql.SQLException;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;

public class buscaTurma extends javax.swing.JDialog {

    public buscaTurma(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        if(!index.LRcurso.isEmpty()){
            jtfCodigo.setText(index.LRcurso.get(0).toString());
        }
    }

    public static boolean ConfirmaTurma;
    public static boolean isConfirmaTurma() {
        return ConfirmaTurma;
    }
    public static void setConfirmaTurma(boolean ConfirmaTurma) {
        buscaTurma.ConfirmaTurma = ConfirmaTurma;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpBTPrincipal = new javax.swing.JPanel();
        jpTabela = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jpBusca = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtfCodigo = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Bucar Turma");

        jpBTPrincipal.setName("jpBTPrincipal"); // NOI18N

        jpTabela.setName("jpTabela"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Fase"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(40);
        jTable1.getColumnModel().getColumn(1).setMaxWidth(40);

        javax.swing.GroupLayout jpTabelaLayout = new javax.swing.GroupLayout(jpTabela);
        jpTabela.setLayout(jpTabelaLayout);
        jpTabelaLayout.setHorizontalGroup(
            jpTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
        );
        jpTabelaLayout.setVerticalGroup(
            jpTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
        );

        jpBusca.setName("jpBusca"); // NOI18N

        jLabel1.setFont(new java.awt.Font("SansSerif", 0, 11));
        jLabel1.setText("Código da Turma:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfCodigo.setFont(new java.awt.Font("SansSerif", 0, 11));
        jtfCodigo.setName("jtfCodigo"); // NOI18N
        jtfCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfCodigoKeyPressed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jButton1.setMargin(new java.awt.Insets(1, 1, 1, 1));
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpBuscaLayout = new javax.swing.GroupLayout(jpBusca);
        jpBusca.setLayout(jpBuscaLayout);
        jpBuscaLayout.setHorizontalGroup(
            jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpBuscaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpBuscaLayout.createSequentialGroup()
                        .addComponent(jtfCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addGap(6, 6, 6)))
                .addContainerGap())
        );
        jpBuscaLayout.setVerticalGroup(
            jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpBuscaLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jpBuscaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(jpBuscaLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jpBTPrincipalLayout = new javax.swing.GroupLayout(jpBTPrincipal);
        jpBTPrincipal.setLayout(jpBTPrincipalLayout);
        jpBTPrincipalLayout.setHorizontalGroup(
            jpBTPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpBusca, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jpTabela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jpBTPrincipalLayout.setVerticalGroup(
            jpBTPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpBTPrincipalLayout.createSequentialGroup()
                .addComponent(jpBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpTabela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpBTPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpBTPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if(evt.getClickCount() == 2){
            setConfirmaTurma(true);
            index.LRturma.add(0,jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString());
            index.LRfase.add(0,jTable1.getValueAt(jTable1.getSelectedRow(), 1).toString());
            setVisible(false);
            dispose();
        }
}//GEN-LAST:event_jTable1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        busca();
}//GEN-LAST:event_jButton1ActionPerformed

    private void busca(){
        String txtsql;
        javax.swing.table.DefaultTableModel jtm = (javax.swing.table.DefaultTableModel)jTable1.getModel();
        for (int x = jTable1.getRowCount() - 1; x >= 0; --x) {
                jtm.removeRow(x);
        }
        try {
            if(!jtfCodigo.getText().isEmpty()){
                txtsql = "and codigo like \""+jtfCodigo.getText()+"%\"";
            }else{
                txtsql = "";
            }
            conexao.Query("select codigo, cod_fase from grade_turma where cod_emp="+index.cod_emp+" "+txtsql+" and ano="+index.periodo.get(0)+" and semestre=\""+index.periodo.get(1)+"\" order by codigo");
            while (conexao.rs.next()) {
                jtm.addRow(new Object[]{conexao.rs.getString("codigo"),conexao.rs.getString("cod_fase")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    private void jtfCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfCodigoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfCodigoKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JPanel jpBTPrincipal;
    private javax.swing.JPanel jpBusca;
    private javax.swing.JPanel jpTabela;
    private javax.swing.JTextField jtfCodigo;
    // End of variables declaration//GEN-END:variables

}
